﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Gp.Scripts.Core;
using Gp.Scripts.Utils;
using Sirenix.OdinInspector;
using UnityEngine;

namespace Gp.Scripts.Data {
    [CreateAssetMenu(fileName = "EnableRuleConfig", menuName = "Gp/规则/规则启用配置", order = 0)]
    public class EnableRuleConfig : ScriptableObject {
        [ListDrawerSettings(ShowFoldout = true, DraggableItems = false, ShowItemCount = false, HideAddButton = true)]
        [LabelText("规则列表")]
        public List<RuleItem> itemList;

        private void OnEnable() {
            List<RuleItem> list = new();
            Dictionary<string, RuleItem> map = itemList.ToDictionary(x => x.actionName, x => x);
            GetType().Assembly
                .GetTargetType(baseClass: typeof(IGameAction), attribute: typeof(ActionRuleAttribute))
                .ForEach(i => {
                    var at = i.GetCustomAttribute<ActionRuleAttribute>();
                    RuleItem temp =
                        map.GetValueOrDefault(at.Name, new RuleItem() {actionName = at.Name, enabled = true});
                    list.Add(temp);
                });
            itemList = list;
        }
    }

    [Serializable]
    public class RuleItem {
        [ToggleLeft(), LabelText("@actionName")] public bool enabled;
        [HideInInspector] public string actionName = "";
    }
}